package com.fingard.dsp.bank.directbank.bocom15;

import com.allinpay.ets.client.StringUtil;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.directbank.bocom13.BOCOM13Header;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.apache.commons.lang.StringEscapeUtils;

import java.util.Date;

public class BOCOM15DeContract extends BOCOM15Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;
        try{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            BOCOM13Header tmpBankHeader = new BOCOM13Header();
            tmpBankHeader.reqSeqID = p_req.contractDetail.askNumber;
            String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_req.contractDetail.recAct);
            if (StringHelper.isNullOrEmpty(corpNo)) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]保险方ID映射,请检查BOCOM15/config.xml中'corpNoMapping'节点配置";
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
            tmpBankHeader.corpNo = corpNo;
            tmpBankHeader.transCode="I00409";
            XmlTextWriter xmlWriterInner = new XmlTextWriter();
            writeCommonHeader(xmlWriterInner, tmpBankHeader);
            xmlWriterInner.writeStartElement("Body");
            xmlWriterInner.writeElementString("Name", p_req.contractDetail.payName);/* 保险方流水号 */
            xmlWriterInner.writeElementString("ActNo", p_req.contractDetail.payAct);/* 投保单号 */
            xmlWriterInner.writeElementString("AgreementNo", p_req.contractDetail.authCode);/* 投保单号 */
            xmlWriterInner.writeElementString("Ext001", "");
            xmlWriterInner.writeElementString("Ext002", "");
            xmlWriterInner.writeElementString("Ext003", "");
            xmlWriterInner.writeElementString("Ext004", "");
            xmlWriterInner.writeElementString("Ext005", "");
            xmlWriterInner.writeElementString("Ext006", "");
            xmlWriterInner.writeElementString("Ext007", "");
            xmlWriterInner.writeElementString("Ext008", "");
            xmlWriterInner.writeElementString("Ext009", "");
            xmlWriterInner.writeElementString("Ext010", "");
            xmlWriterInner.writeEndDocument();
            txCode = "ECIP000189";
            reqSeqID = p_req.contractDetail.askNumber;
            writeCommonHeader(xmlWriter);
            String xml = xmlWriterInner.toXmlString();
            String mac = getBankConfig().getValueAsString("mac");
            WriteBankLogLn("MAC值:"+mac);
            String headLenth = String.format("%8s", mac.length()+xml.length()+"<?xml version=\"1.0\" encoding=\"UTF-8\"?>".length()).replace(" ", "0");
            WriteBankLogLn("报文长:"+headLenth);
            String head = headLenth+mac;
            xmlWriter.writeElementString("Transaction_Head",head);
            xmlWriter.writeElementString("Transaction_Body",StringEscapeUtils.unescapeXml(xml));
            xmlWriter.writeEndDocument();//Transaction_Body
            String[] tmpStrRet = sendToBank(StringEscapeUtils.unescapeXml(xmlWriter.toXmlString()));
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String returnMsg = tmpBkReader.getFirstText("/TX/TX_BODY/ENTITY/Transaction_Body");
                XmlTextReader tmpBkReaders = loadBankRespXmlReader(returnMsg, tmpSbMsg);
                if (tmpBkReaders != null) {
                    tmpRetDetail.respCode = "0";
                    tmpRetDetail.respInfo = tmpBkReaders.getFirstText("/Rsp/Head/ErrMsg");
                    String tmpMsgType = tmpBkReaders.getFirstText("/Rsp/Head/MsgType");
                    String tmpErrCode = tmpBkReaders.getFirstText("/Rsp/Head/ErrCode");
                    if (!"N".equals(tmpMsgType) && !"200095".equals(tmpErrCode)) {
                        tmpRetDetail.respCode = "-1";
                    }
                    if ("000000".equals(tmpErrCode) || "200095".equals(tmpErrCode)) {
                        tmpRetDetail.deSignState = ContractState.SUCCESS;
                    } else {
                        tmpRetDetail.deSignState = ContractState.FAIL;
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpSbMsg.toString();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
